//
//  DetailJXKCViewController.m
//  KuWoCan
//
//  Created by hwd on 17/3/6.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "DetailJXKCViewController.h"
#import "DetailJXCCCell.h"
#import "NWESSRequest.h"
#import "ESSListModel.h"
#import "NWEnquiryPlanRequest.h"

@interface DetailJXKCViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableV;

@property (nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation DetailJXKCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (_businessType == BusinessTypeEnquirPlan)
    {
        //要货计划
        [self setTitle:@"要货计划明细"];
        NWEnquiryPlanRequest *request = [[NWEnquiryPlanRequest alloc] init];
        [request startRequestPlanDetailWithCargoPlanId:_cargoPlanId];
        [request completion:^(id result, NSString *message, id extension, BOOL succ) {
            _dataArr = [ESSListModel arrayOfModelsFromDictionaries:result];
            
            
            [self.view addSubview:self.addTableView];
        }];
    }
    else
    {
        [self setTitle:@"进销详情"];
        NWESSRequest *request = [[NWESSRequest alloc] init];
        [request startRequestGetESSDetailWithInvoicingId:_invoicingId];
        [request completion:^(id result, NSString *message, id extension, BOOL succ) {
            _dataArr = [ESSListModel arrayOfModelsFromDictionaries:result];
            
            
            [self.view addSubview:self.addTableView];
        }];
    }
    
    
}

-(UITableView *)addTableView//添加TableView
{
    if (!_tableV) {
        _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0,0,SCREENW,SCREENH - 64) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = [UIColor colorWithHexString:@"F6F7F8"];
        _tableV.dataSource = self;
        _tableV.delegate = self;
        _tableV.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tableV;
    
}
#pragma mark - TableView专区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110*screenW_6;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailJXCCCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell%d",(int)indexPath.row]];
    if (cell == nil) {
        cell  = [[DetailJXCCCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%d",(int)indexPath.row]];
    }
    [cell loadCellWith:nil];
    if (_businessType == BusinessTypeESS)
    {
        cell.model = _dataArr[indexPath.row];
    }
    else
    {
        cell.planModel = _dataArr[indexPath.row];
    }
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 40*screenW_6)];
    footView.backgroundColor = [UIColor whiteColor];
    [footView addSubview:[ToolViewViewController addCellLineLab:CGRectMake(0, 0,SCREENW,1*screenW_6)]];
    
    UILabel *heJiLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREENW-180*screenW_6, 1, 165*screenW_6, 39*screenW_6)];
    heJiLab.textAlignment = NSTextAlignmentRight;
    heJiLab.font = [UIFont systemFontOfSize:14*screenW_6];
    heJiLab.textColor = [UIColor colorWithHexString:@"55aee9"];
    
    NSInteger totalBoxNum = 0;
    for (int i = 0; i<_dataArr.count; i++)
    {
        ESSListModel *model = _dataArr[i];
        totalBoxNum = totalBoxNum + [model.quantity integerValue];
    }
    
    heJiLab.text = [NSString stringWithFormat:@"合计：%ld箱",(long)totalBoxNum];
    [footView addSubview:heJiLab];
    
    return footView;
}

@end
